package 树.线索二叉树;

public class ThreadBinaryTreeDemo {
    public static void main(String[] args) {

    }
}

// 定义ThreadBinaryTree 实现了线索化功能的二叉树
class BinaryTree{
    HeroNode root;

    public BinaryTree(HeroNode head){
        this.root = head;
    }

    // 删除节点
    public void delNode(int no){
        if(root != null){
            if(root.getNo() == no){
                root = null;
            }else{
                root.delNode(no);
            }
        }else{
            System.out.println("二叉树为空,无法进行删除");
        }
    }

    // 前序查找
    public HeroNode preOrderSearch(int value){
        if(this.root == null){
            return null;
        }else{
            return this.root.preOrderSearch(value);
        }
    }

    // 中序查找
    public HeroNode infixOrderSearch(int value){
        if(this.root == null){
            return null;
        }else{
            return this.root.infixOrderSearch(value);
        }
    }

    // 后续查找
    public HeroNode postOrderSearch(int value){
        if(this.root == null){
            return null;
        }else{
            return this.root.postOrderSearch(value);
        }
    }

    // 前序遍历
    public void preOrder(){
        if(this.root != null){
            this.root.preOrder();
        }else{
            System.out.println("当前二叉树为空,无法遍历");
        }
    }

    // 中序遍历
    public void infixOrder() {
        if (this.root != null) {
            this.root.getLeft().infixOrder();
        }else{
            System.out.println("当前二叉树为空,无法遍历");
        }
    }

    // 后序遍历
    public void postOrder() {
        if (this.root != null) {
            this.root.getLeft().postOrder();
        }else{
            System.out.println("当前二叉树为空,无法遍历");
        }
    }

    public void delete(){

    }

}

class HeroNode{
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    private int leftType;// 为零,表示指向左节点 为一,表示指向前驱
    private int rightType;

    public HeroNode(int no,String name){
        super();
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    // 删除节点
    public void delNode(int no){
        if(this.left != null && this.left.no == no){
            this.left = null;
            return;
        }

        if(this.right != null && this.right.no == no){
            this.right = null;
            return;
        }
        if(this.left != null) {
            this.left.delNode(no);
        }
        if(this.right != null){
            this.right.delNode(no);
        }
    }

    /**
     *
     * @param value 要查找的值
     * @return 找到了就返回该节点,找不到就返回空
     */
    public HeroNode preOrderSearch(int value) {
        if (this.no == value) {
            return this;
        }
        HeroNode node = null;
        if (this.left != null) {
            node =  this.left.preOrderSearch(value);
        }
        if(node != null){// node 不为空,说明找到了
            return node;
        }

        if (this.right != null) {
            node =  this.right.preOrderSearch(value);
        }
        return node;
    }

    // 中序查找
    public HeroNode infixOrderSearch(int value){
        HeroNode node = null;
        if (this.left != null) {
            node = this.left.preOrderSearch(value);
        }
        if(node != null){
            return node;
        }
        if (this.no == value) {
            return this;
        }

        if (this.right != null) {
            node = this.right.preOrderSearch(value);
        }
        // 找不到,返回空
        return node;
    }

    // 后序查找
    public HeroNode postOrderSearch(int value){
        HeroNode node = null;
        if(this.left != null){
            node = this.left.postOrderSearch(value);
        }
        if(node != null){
            return node;
        }
        if(this.right != null){
            node = this.right.postOrderSearch(value);
        }
        if(this.no == value){
            return this;
        }
        return node;
    }

    // 前序遍历
    public void preOrder(){
        System.out.println(this);// 先输出父节点
        if(this.left != null){
            this.left.preOrder();
        }
        if(this.right != null){
            this.right.preOrder();
        }
    }
    // 中序遍历
    public void infixOrder(){
        if(this.left != null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if(this.right != null){
            this.right.infixOrder();
        }
    }

    // 后序遍历
    public void postOrder(){
        if(this.left != null){
            this.left.postOrder();
        }
        if(this.right != null){
            this.right.postOrder();
        }
        System.out.println(this);
    }
}
